package com.qishao.service_base.reponse;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author： xushaoqi
 * @Date：2021/8/13
 */
@Data
public class Response {

    @ApiModelProperty(value = "成功与否")
    private Boolean success;

    @ApiModelProperty(value = "状态码")
    private Integer code;

    @ApiModelProperty(value = "响应信息")
    private String message;

    @ApiModelProperty(value = "数据集")
    private Map<String, Object> data = new HashMap<>();


    private Response() {
    }

    public Response(Integer code, String message, Map<String, Object> data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static Response ok() {
        Response rep = new Response();
        rep.setCode(ResponseCode.SUCCESS);
        rep.setMessage("success");
        rep.setSuccess(true);
        return rep;
    }

    public static Response ok(Object obj) {
        Response rep = new Response();
        rep.setCode(ResponseCode.SUCCESS);
        rep.setMessage("success");
        rep.setSuccess(true);
        rep.getData().put("data",obj);
        return rep;
    }

    public static Response ok(String message) {
        Response rep = new Response();
        rep.setCode(ResponseCode.SUCCESS);
        rep.setMessage(message);
        rep.setSuccess(true);
        return rep;
    }

    public static Response ok(String message, Map<String, Object> data) {
        Response rep = new Response();
        rep.setCode(ResponseCode.SUCCESS);
        rep.setMessage(message);
        rep.setSuccess(true);
        rep.setData(data);
        return rep;
    }


    public static Response error() {
        Response rep = new Response();
        rep.setCode(ResponseCode.ERROR);
        rep.setMessage("error");
        rep.setSuccess(false);
        return rep;
    }

    public static Response error(String message) {
        Response rep = new Response();
        rep.setCode(ResponseCode.ERROR);
        rep.setMessage(message);
        rep.setSuccess(false);
        return rep;
    }

    public static Response messsage(String message) {
        Response rep = new Response();
        rep.setMessage(message);
        return rep;
    }


    public Response data(String key,Object value){
        this.data.put(key,value);
        return this;
    }

    public Response data(Map<String,Object> data){
        this.data = data;
        return this;
    }

    public Response message(String message){
        this.message = message;
        return this;
    }

    public Response code(Integer code){
        this.code = code;
        return this;
    }

}
